#pragma once

#include <httplib.h>
#include <thread>
#include <atomic>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <vector>
#include <memory>

#include "common.h"
#include "thread_pool.h"
#include "db_handler.h"
#include "dish_module.h"
#include "order_module.h"
#include "cart_module.h"

namespace TakeAwayPlatform
{
    class RestServer 
    {
    public:
        RestServer(DatabaseHandler& dbHandler, DishModule& dishModule, OrderModule& orderModule, CartModule& cartModule);
        ~RestServer();

        void start(int port);
        void stop();
        bool is_running() const;

    private:
        void run_server(int port);
        void setup_routes();

        Json::Value parse_json(const std::string& jsonStr);

        DatabaseHandler& dbHandler;
        DishModule& dishModule;
        OrderModule& orderModule;
        CartModule& cartModule;

        httplib::Server server;
        ThreadPool threadPool;
        std::atomic<bool> isRunning {false};
        std::atomic<bool> stopRequested {false};

        std::mutex stopMtx;
        std::condition_variable stopCv;

        std::thread serverThread;
    };
}